package com.hmyd.hmyd_android.ui;

import java.math.BigDecimal;
import java.util.Calendar;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TableLayout;
import android.widget.TextView;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.utils.MyToast;
import com.hmyd.hmyd_android.utils.Utils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * 收益计算器
 * 
 * @author wangc
 * 
 */
@ContentView(R.layout.activity_calculator)
public class CalculatorActivity extends BasicActivity implements View.OnClickListener {
	// 标题栏设置
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;
	// 投资本金
	@ViewInject(R.id.caculator_money)
	private EditText mEdMoney;
	//投资期限RadioGroup管理器
	@ViewInject(R.id.date_group)
	private RadioGroup date_group;
	//投资期限选择--天
	@ViewInject(R.id.day_date_btn)
	private RadioButton day_date_btn;
	//投资期限选择--月
	@ViewInject(R.id.month_date_btn)
	private RadioButton month_date_btn;
	// 投资期限
	@ViewInject(R.id.caculator_month)
	private EditText mEdMonth;
	//投资期限后的文字
	@ViewInject(R.id.tv_yue)
	private TextView tv_yue;
	// 年化利率
	@ViewInject(R.id.caculator_percent)
	private EditText mEdPercent;
	// 预期收益
	@ViewInject(R.id.tv_expected_return)
	private TextView mTvExpectedReturn;
	// 实际年化利率
	@ViewInject(R.id.tv_real_return)
	private TextView mTvRealReturn;
	// 现金券
	@ViewInject(R.id.ed_cash_coupon)
	private EditText mEdCashCoupon;
	// 小惠钱包
	@ViewInject(R.id.ed_wallet)
	private EditText mEdWallet;
	// 用券模式
	@ViewInject(R.id.checkbox)
	private CheckBox mCheckBox;
	// 用券
	@ViewInject(R.id.layout_cash_coupon_and_wallet)
	private LinearLayout mCashWalletLayout;
	// 回款计划
	@ViewInject(R.id.back_section_table)
	private TableLayout mBackPlanLayout;
	@ViewInject(R.id.btn_calcultor)
	private Button btn_calcultor;

	// 期限
	private int mPeriod;
	// 利率
	private String mRate;
	// 本金
	private double mMount;
	// 现金券
	private String cashCoupon;
	// 小惠钱包
	private String walletMount;
	private View view;
	BigDecimal cash;
	BigDecimal wallet;
	BigDecimal ccBigDecimal;

	// 计算
//	@OnClick(R.id.btn_calcultor)
	public void onClick(View view){
		switch (view.getId()) {
		case R.id.btn_calcultor:
			if (day_date_btn.isChecked()){
				calcultorDay(day_date_btn);
			} else if (month_date_btn.isChecked()) {
				calcultor(month_date_btn);
			}
			break;

		default:
			break;
		}
	}
	public void calcultor(View view) {
		VerifyMount();
		VerifyPeriod();
		VerifyRate();

		try {
			BigDecimal t = new BigDecimal(String.valueOf(mPeriod));	//	期限
			BigDecimal investAmount = new BigDecimal((mEdMoney.getText().toString().trim()));	//本金
			BigDecimal r = new BigDecimal((mRate));	//利率
			// 预期收益
			String expectedReturn = String.valueOf(Utils.scale(investAmount.multiply(r)
					.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_DOWN)
					.divide(new BigDecimal("12"), 2, BigDecimal.ROUND_DOWN).multiply(t)));// +优惠券);
			mTvExpectedReturn.setText(expectedReturn);
			BigDecimal ex = investAmount.multiply(r)
					.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_DOWN)
					.divide(new BigDecimal("12"), 2, BigDecimal.ROUND_DOWN).multiply(t);

			// 实际年化利率:(合计利息+优惠券)/实际本金*12/期限

			if (!TextUtils.isEmpty(mEdCashCoupon.getText().toString().trim())
					&& !TextUtils.isEmpty(mEdWallet.getText().toString().trim())) {
				cashCoupon = mEdCashCoupon.getText().toString().trim();
				cash = new BigDecimal(cashCoupon);
				walletMount = mEdWallet.getText().toString().trim();
				wallet = new BigDecimal(walletMount);
				BigDecimal aaBigDecimal = ex.add(cash).add(wallet);
				BigDecimal bbBigDecimal = (investAmount.subtract(cash.add(wallet)));
				ccBigDecimal = aaBigDecimal.divide(bbBigDecimal, 6, BigDecimal.ROUND_DOWN)
						.multiply(new BigDecimal("12")).divide(t, 6, BigDecimal.ROUND_DOWN);

				mTvRealReturn.setText(String.valueOf(ccBigDecimal.multiply(new BigDecimal("100"))
						.divide(new BigDecimal("1"), 2, BigDecimal.ROUND_DOWN)));
			}

			if (!TextUtils.isEmpty(mEdWallet.getText().toString().trim())
					&& TextUtils.isEmpty(mEdCashCoupon.getText().toString().trim())) {
				walletMount = mEdWallet.getText().toString().trim();
				wallet = new BigDecimal(walletMount);
				BigDecimal aaBigDecimal = ex.add(wallet);
				BigDecimal bbBigDecimal = (investAmount.subtract(wallet));
				BigDecimal ccBigDecimal = aaBigDecimal
						.divide(bbBigDecimal, 6, BigDecimal.ROUND_DOWN)
						.multiply(new BigDecimal("12")).divide(t, 6, BigDecimal.ROUND_DOWN);

				mTvRealReturn.setText(String.valueOf(ccBigDecimal.multiply(new BigDecimal("100"))
						.divide(new BigDecimal("1"), 2, BigDecimal.ROUND_DOWN)));
			}

			if (TextUtils.isEmpty(mEdWallet.getText().toString().trim())
					&& !TextUtils.isEmpty(mEdCashCoupon.getText().toString().trim())) {
				cashCoupon = mEdCashCoupon.getText().toString().trim();
				cash = new BigDecimal(cashCoupon);
				BigDecimal aaBigDecimal = ex.add(cash);
				BigDecimal bbBigDecimal = (investAmount.subtract(cash));
				ccBigDecimal = aaBigDecimal.divide(bbBigDecimal, 6, BigDecimal.ROUND_DOWN)
						.multiply(new BigDecimal("12")).divide(t, 6, BigDecimal.ROUND_DOWN);

				mTvRealReturn.setText(String.valueOf(ccBigDecimal.multiply(new BigDecimal("100"))
						.divide(new BigDecimal("1"), 2, BigDecimal.ROUND_DOWN)));
			}

			if (TextUtils.isEmpty(mEdWallet.getText().toString().trim())
					&& TextUtils.isEmpty(mEdCashCoupon.getText().toString().trim())) {
				mTvRealReturn.setText(mRate);
			}

			if (!mCheckBox.isChecked()) {
				mTvRealReturn.setText(mRate);
				mEdCashCoupon.setText("");
				mEdWallet.setText("");
			}

		} catch (Exception e) {

		}

		initBackSectionTable(mRate, String.valueOf(mPeriod), "Month", mMount,
				System.currentTimeMillis());

	}
	
	//按天计算
		public void calcultorDay(View view) {
			VerifyMount();
			VerifyPeriodDay();
			VerifyRate();

			try {
				BigDecimal t = new BigDecimal(String.valueOf(mPeriod));
				BigDecimal investAmount = new BigDecimal((mEdMoney.getText().toString().trim()));
				BigDecimal r = new BigDecimal((mRate));
				// 预期收益
				String expectedReturn = String.valueOf(Utils.scale(investAmount.multiply(r)
						.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_DOWN)
						.divide(new BigDecimal("365"), 2, BigDecimal.ROUND_DOWN).multiply(t)));// +优惠券);
				mTvExpectedReturn.setText(expectedReturn);
				BigDecimal ex = investAmount.multiply(r)
						.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_DOWN)
						.divide(new BigDecimal("365"), 2, BigDecimal.ROUND_DOWN).multiply(t);

				// 实际年化利率:(合计利息+优惠券)/实际本金*12/期限

				if (!TextUtils.isEmpty(mEdCashCoupon.getText().toString().trim())
						&& !TextUtils.isEmpty(mEdWallet.getText().toString().trim())) {
					cashCoupon = mEdCashCoupon.getText().toString().trim();
					cash = new BigDecimal(cashCoupon);
					walletMount = mEdWallet.getText().toString().trim();
					wallet = new BigDecimal(walletMount);
					BigDecimal aaBigDecimal = ex.add(cash).add(wallet);
					BigDecimal bbBigDecimal = (investAmount.subtract(cash.add(wallet)));
					ccBigDecimal = aaBigDecimal.divide(bbBigDecimal, 6, BigDecimal.ROUND_DOWN)
							.multiply(new BigDecimal("365")).divide(t, 6, BigDecimal.ROUND_DOWN);

					mTvRealReturn.setText(String.valueOf(ccBigDecimal.multiply(new BigDecimal("100"))
							.divide(new BigDecimal("1"), 2, BigDecimal.ROUND_DOWN)));
				}

				if (!TextUtils.isEmpty(mEdWallet.getText().toString().trim())
						&& TextUtils.isEmpty(mEdCashCoupon.getText().toString().trim())) {
					walletMount = mEdWallet.getText().toString().trim();
					wallet = new BigDecimal(walletMount);
					BigDecimal aaBigDecimal = ex.add(wallet);
					BigDecimal bbBigDecimal = (investAmount.subtract(wallet));
					BigDecimal ccBigDecimal = aaBigDecimal
							.divide(bbBigDecimal, 6, BigDecimal.ROUND_DOWN)
							.multiply(new BigDecimal("365")).divide(t, 6, BigDecimal.ROUND_DOWN);

					mTvRealReturn.setText(String.valueOf(ccBigDecimal.multiply(new BigDecimal("100"))
							.divide(new BigDecimal("1"), 2, BigDecimal.ROUND_DOWN)));
				}

				if (TextUtils.isEmpty(mEdWallet.getText().toString().trim())
						&& !TextUtils.isEmpty(mEdCashCoupon.getText().toString().trim())) {
					cashCoupon = mEdCashCoupon.getText().toString().trim();
					cash = new BigDecimal(cashCoupon);
					BigDecimal aaBigDecimal = ex.add(cash);
					BigDecimal bbBigDecimal = (investAmount.subtract(cash));
					ccBigDecimal = aaBigDecimal.divide(bbBigDecimal, 6, BigDecimal.ROUND_DOWN)
							.multiply(new BigDecimal("365")).divide(t, 6, BigDecimal.ROUND_DOWN);

					mTvRealReturn.setText(String.valueOf(ccBigDecimal.multiply(new BigDecimal("100"))
							.divide(new BigDecimal("1"), 2, BigDecimal.ROUND_DOWN)));
				}

				if (TextUtils.isEmpty(mEdWallet.getText().toString().trim())
						&& TextUtils.isEmpty(mEdCashCoupon.getText().toString().trim())) {
					mTvRealReturn.setText(mRate);
				}

				if (!mCheckBox.isChecked()) {
					mTvRealReturn.setText(mRate);
					mEdCashCoupon.setText("");
					mEdWallet.setText("");
				}
				if (!day_date_btn.isChecked()){
					mTvRealReturn.setText(mRate);
					mEdCashCoupon.setText("");
					mEdWallet.setText("");
				}

			} catch (Exception e) {

			}

			initBackSectionTable(mRate, String.valueOf(mPeriod), "Day", mMount,
					System.currentTimeMillis());

		}

	// 重置
	@OnClick(R.id.btn_reset)
	public void reset(View view) {
		mEdMoney.setText("");
		mEdMonth.setText("");
		mEdPercent.setText("");
		mEdWallet.setText("");
		mEdCashCoupon.setText("");
		mTvExpectedReturn.setText("");
		mTvRealReturn.setText("");
	}

	// 查看回款计划
	@OnClick(R.id.tv_back_plan)
	public void backPlan(View view) {
		if (mBackPlanLayout.getVisibility() == View.VISIBLE) {
			mBackPlanLayout.setVisibility(View.GONE);
		} else if (mBackPlanLayout.getVisibility() == View.GONE) {
			mBackPlanLayout.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 验证本金
	 */
	private void VerifyMount() {
		String amount = mEdMoney.getText().toString().trim();
		if (amount.matches("^[0-9]+(.[0-9]{2})?$") || amount.matches("^[0-9]+(.[0-9]{1})?$")
				|| amount.matches("^[0-9]")) {
			mMount = Double.valueOf(amount);
			if (mMount < 1 || mMount > 1000000000) {
//				Toast.makeText(getApplicationContext(),
//						getString(R.string.investment_amount_illegal), Toast.LENGTH_SHORT).show();
				MyToast.showHMToast(this, false, getString(R.string.investment_amount_illegal));
				if (mBackPlanLayout != null) {
					mBackPlanLayout.removeAllViews();
				}
				mTvExpectedReturn.setText("0.0");
				return;
			}
		} else {
//			Toast.makeText(getApplicationContext(), getString(R.string.investment_amount_illegal),
//					Toast.LENGTH_SHORT).show();
			MyToast.showHMToast(this, false, getString(R.string.investment_amount_illegal));
			if (mBackPlanLayout != null) {
				mBackPlanLayout.removeAllViews();
			}
			mTvExpectedReturn.setText("0.0");
			return;
		}
	}

	/**
	 * 验证利率
	 */
	private void VerifyRate() {
		mRate = mEdPercent.getText().toString().trim();
		if (TextUtils.isEmpty(mRate) || TextUtils.isEmpty(mEdMonth.getText().toString().trim())
				|| TextUtils.isEmpty(mEdMoney.getText().toString().trim())) {
			return;
		}

		if (mRate.matches("^[0-9]+(.[0-9]{2})?$") || mRate.matches("^[0-9]+(.[0-9]{1})?$")
				|| mRate.matches("^[0-9]")) {
			if (Double.valueOf(mRate) < 0 || Double.valueOf(mRate) > 200) {
//				Toast.makeText(getApplicationContext(),
//						getString(R.string.investment_rate_illegal), Toast.LENGTH_SHORT).show();
				MyToast.showHMToast(CalculatorActivity.this, false, getString(R.string.investment_rate_illegal));
				if (mBackPlanLayout != null) {
					mBackPlanLayout.removeAllViews();
				}
				mTvExpectedReturn.setText("0.0");
				return;
			}
		} else {
//			Toast.makeText(getApplicationContext(), getString(R.string.investment_rate_illegal),
//					Toast.LENGTH_SHORT).show();
			MyToast.showHMToast(CalculatorActivity.this, false, getString(R.string.investment_rate_illegal));
			if (mBackPlanLayout != null) {
				mBackPlanLayout.removeAllViews();
			}
			mTvExpectedReturn.setText("0.0");
			return;
		}

	}

	/**
	 * 验证投资期限(按月)
	 */
	private void VerifyPeriod() {
		if (TextUtils.isEmpty(mEdMonth.getText().toString().trim())) {
			// MyToast.showHMToast(CalculatorActivity.this, false, "请输入投资期限");
			return;
		}
		mPeriod = Integer.valueOf(mEdMonth.getText().toString().trim());
		if (mPeriod < 1 || mPeriod > 60) {
//			Toast.makeText(getApplicationContext(), getString(R.string.investment_period_illegal),
//					Toast.LENGTH_SHORT).show();
			MyToast.showHMToast(CalculatorActivity.this, false, getString(R.string.investment_period_illegal));
			if (mBackPlanLayout != null) {
				mBackPlanLayout.removeAllViews();
			}
			mTvExpectedReturn.setText("0.0");
			return;
		}

	}
	
	/**
	 * 验证投资期限(按天)
	 */
	private void VerifyPeriodDay() {
		if (TextUtils.isEmpty(mEdMonth.getText().toString().trim())) {
			// MyToast.showHMToast(CalculatorActivity.this, false, "请输入投资期限");
			return;
		}
		mPeriod = Integer.valueOf(mEdMonth.getText().toString().trim());
		if (mPeriod < 1 || mPeriod > 365) {
//			Toast.makeText(getApplicationContext(), getString(R.string.investment_period_illegal),
//					Toast.LENGTH_SHORT).show();
			MyToast.showHMToast(CalculatorActivity.this, false, getString(R.string.investment_period_illegal_day));
			if (mBackPlanLayout != null) {
				mBackPlanLayout.removeAllViews();
			}
			mTvExpectedReturn.setText("0.0");
			return;
		}

	}

	@Override
	public void onPostSuccess(int postId, String msg) {

	}

	@Override
	public void onPostFailure(int postId, String msg) {

	}

	@Override
	protected void setParams() {
		mPeriod = getIntent().getIntExtra("period", 1);
		mRate = getIntent().getStringExtra("rate");
		if (!TextUtils.isEmpty(mRate)) {
			mEdPercent.setText(mRate);
			mEdMonth.setText(mPeriod+"");
		}

		home_title_txt.setText(getResources().getString(R.string.calculator));
		home_right.setText(getResources().getString(R.string.investment_now));
		home_title.setOnTouchListener(touch);
		btn_calcultor.setOnClickListener(this);

		view = mBackPlanLayout.getChildAt(0);
		mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					mCashWalletLayout.setVisibility(View.VISIBLE);
				} else {
					mCashWalletLayout.setVisibility(View.GONE);
				}
			}
		});
		
		date_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == month_date_btn.getId()) {
					tv_yue.setText("月");
					calcultor(tv_yue);
				} else if (checkedId == day_date_btn.getId()){
					tv_yue.setText("天");
					calcultorDay(tv_yue);
				}
			}

		});

//		mEdPercent.addTextChangedListener(new TextWatcher() {
//
//			@Override
//			public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//			}
//
//			@Override
//			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//			}
//
//			@Override
//			public void afterTextChanged(Editable s) {
//				VerifyRate();
//			}
//		});
//
//		mEdMoney.addTextChangedListener(new TextWatcher() {
//
//			@Override
//			public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//			}
//
//			@Override
//			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//			}
//
//			@Override
//			public void afterTextChanged(Editable s) {
//				VerifyMount();
//			}
//		});
//
//		mEdMonth.addTextChangedListener(new TextWatcher() {
//
//			@Override
//			public void onTextChanged(CharSequence s, int start, int before, int count) {
//
//			}
//
//			@Override
//			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//			}
//
//			@Override
//			public void afterTextChanged(Editable s) {
//				VerifyPeriod();
//			}
//		});

	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {
		Intent intent = new Intent();
		intent.putExtra("key", "investment");
		intent.setClass(CalculatorActivity.this, MainActivity.class);
		startActivity(intent);
	}

	private void initBackSectionTable(String rate, String time, String timeUnit, double mount,
			long endTime) {

		if (mBackPlanLayout != null) {
			mBackPlanLayout.removeAllViews();
		}
		mBackPlanLayout.addView(view);
		if (TextUtils.isEmpty(rate)) {
			return;
		}
		BigDecimal t = new BigDecimal(time);
		BigDecimal investAmount = new BigDecimal(String.valueOf(mount));
		BigDecimal r = new BigDecimal(rate);

		LayoutInflater mInflater = LayoutInflater.from(this);

		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(endTime);
		c.add(Calendar.DAY_OF_MONTH, 0);
		c.add(Calendar.MONTH, 1);

		for (int i = 0; i < t.intValue(); i++) {
			View lineView = mInflater.inflate(R.layout.view_investment_back_section_line, null);
			mBackPlanLayout.addView(lineView);

			View sectionView = mInflater.inflate(R.layout.view_investment_back_section, null);
			TextView date = (TextView) sectionView.findViewById(R.id.date);
			TextView back = (TextView) sectionView.findViewById(R.id.back);
			TextView principal = (TextView) sectionView.findViewById(R.id.principal);
			TextView total = (TextView) sectionView.findViewById(R.id.total);

			date.setTextColor(getResources().getColor(R.color.recommendation_sidemenu_bg));
			back.setTextColor(getResources().getColor(R.color.recommendation_sidemenu_bg));
			principal.setTextColor(getResources().getColor(R.color.recommendation_sidemenu_bg));
			total.setTextColor(getResources().getColor(R.color.recommendation_sidemenu_bg));

			BigDecimal mountBigDecimal;
			if ((i + 1) == t.intValue()) {
				principal.setText(Utils.getFloatDotStr(investAmount.toString()));
				mountBigDecimal = new BigDecimal(String.valueOf(mount));
			} else {
				principal.setText("0");
				mountBigDecimal = new BigDecimal(String.valueOf(0));
			}

			if ("Year".equals(timeUnit)) {
				back.setText(String.valueOf(Utils.scale(investAmount.multiply(r).divide(
						new BigDecimal("100"), 2, BigDecimal.ROUND_DOWN))));
				total.setText(String.valueOf(Utils.scale(investAmount.multiply(r)
						.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_DOWN)
						.add(mountBigDecimal))));
			} else if ("Month".equals(timeUnit)) {
				back.setText(String.valueOf(Utils.scale(investAmount.multiply(r)
						.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_DOWN)
						.divide(new BigDecimal("12"), 2, BigDecimal.ROUND_DOWN))));
				total.setText(String.valueOf(Utils.scale(investAmount.multiply(r)
						.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_DOWN)
						.divide(new BigDecimal("12"), 2, BigDecimal.ROUND_DOWN)
						.add(mountBigDecimal))));
			} else if ("Day".equals(timeUnit)) {
				back.setText(String.valueOf(Utils.scale(investAmount.multiply(r)
						.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_DOWN)
						.divide(new BigDecimal("365"), 2, BigDecimal.ROUND_DOWN))));
				total.setText(String.valueOf(Utils.scale(investAmount.multiply(r)
						.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_DOWN)
						.divide(new BigDecimal("365"), 2, BigDecimal.ROUND_DOWN)
						.add(mountBigDecimal))));
			}

			date.setText(c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-"
					+ c.get(Calendar.DAY_OF_MONTH));
			date.setTextColor(getResources().getColor(R.color.recommendation_sidemenu_bg));

			mBackPlanLayout.addView(sectionView);

			// 添加天和月的换算
			if (timeUnit.equalsIgnoreCase("Day")) {
				c.add(Calendar.DAY_OF_MONTH, 1);
			} else if(timeUnit.equalsIgnoreCase("Month")){
				c.add(Calendar.MONTH, 1);
			}
		}

		View lineView = mInflater.inflate(R.layout.view_investment_back_section_line, null);
		mBackPlanLayout.addView(lineView);

		View totalView = mInflater.inflate(R.layout.view_investment_back_section_total, null);
		TextView back = (TextView) totalView.findViewById(R.id.back);
		TextView principal = (TextView) totalView.findViewById(R.id.principal);
		TextView total = (TextView) totalView.findViewById(R.id.total);

		back.setTextColor(getResources().getColor(R.color.recommendation_sidemenu_bg));
		principal.setTextColor(getResources().getColor(R.color.recommendation_sidemenu_bg));
		total.setTextColor(getResources().getColor(R.color.recommendation_sidemenu_bg));

		if ("Year".equals(timeUnit)) {
			back.setText(String.valueOf(Utils.scale(investAmount.multiply(r)
					.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_DOWN).multiply(t))));
			total.setText(String.valueOf(Utils.scale(investAmount.multiply(r)
					.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_DOWN).multiply(t)
					.add(new BigDecimal(mount)))));
		} else if ("Month".equals(timeUnit)) {
			back.setText(String.valueOf(Utils.scale(investAmount.multiply(r)
					.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_DOWN)
					.divide(new BigDecimal("12"), 2, BigDecimal.ROUND_DOWN).multiply(t))));
			total.setText(String.valueOf(Utils.scale(investAmount.multiply(r)
					.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_DOWN)
					.divide(new BigDecimal("12"), 2, BigDecimal.ROUND_DOWN).multiply(t)
					.add(new BigDecimal(String.valueOf(mount))))));
		} else if ("Day".equals(timeUnit)) {
			back.setText(String.valueOf(Utils.scale(investAmount.multiply(r)
					.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_DOWN)
					.divide(new BigDecimal("365"), 2, BigDecimal.ROUND_DOWN).multiply(t))));
			total.setText(String.valueOf(Utils.scale(investAmount.multiply(r)
					.divide(new BigDecimal("100"), 2, BigDecimal.ROUND_DOWN)
					.divide(new BigDecimal("365"), 2, BigDecimal.ROUND_DOWN).multiply(t)
					.add(new BigDecimal(String.valueOf(mount))))));
		}

		principal.setText(Utils.getFloatDotStr(investAmount.toString()));
		principal.setTextColor(getResources().getColor(R.color.recommendation_sidemenu_bg));

		mBackPlanLayout.addView(totalView);
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		MyToast.closeToast(CalculatorActivity.this);
	}

}
